package com.lt.service.impl;

import com.lt.entity.Student;
import com.lt.entity.Teacher;
import com.lt.mapper.StudentMapper;
import com.lt.mapper.TeacherMapper;
import com.lt.service.StuAndTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author :liteng
 * @Time : 2023/11/4 16:38
 */

@Service
@Slf4j
public class StuAndTeacherServiceImpl implements StuAndTeacherService {

    private final TeacherMapper teacherMapper;

    private final StudentMapper studentMapper;

    public StuAndTeacherServiceImpl(TeacherMapper teacherMapper, StudentMapper studentMapper) {
        this.teacherMapper = teacherMapper;
        this.studentMapper = studentMapper;
    }

    /**
     * 插入学生，返回主键
     * @param student
     * @return
     */
    @Override
    public Long addStu(Student student) {
        Long id;
        if (!studentMapper.existStu(student.getStuNum())) {
            studentMapper.insertStu(student);
            id = student.getId();
        } else {
            id = studentMapper.selectStuId(student.getStuNum());
            log.info("学号已存在");
        }
        return id;
    }

    @Override
    public Boolean addStudents(List<Student> students, Long projectId) {
        return null;
    }

    /**
     * 插入一条老师记录
     *
     * @param teacher
     * @return 老师主键
     */
    @Override
    public Long addTeacher(Teacher teacher) {
        Long id;
        if (!teacherMapper.existTeacher(teacher.getTeacherNum())) {
            teacherMapper.insertTeacher(teacher);
            id = teacher.getId();
        } else {
            id = teacherMapper.selectTeacherId(teacher.getTeacherNum());
            log.info("教师号已存在");
        }
        return id;
    }

    @Override
    public Student findByStuNum(Long stuNum) {
        Student student = studentMapper.selectStu(stuNum);
        return student;
    }

    @Override
    public Teacher findByTeacherNum(Long teacherNUm) {
        Teacher teacher = teacherMapper.selectTeacherByNum(teacherNUm);
        return teacher;
//        return null;
    }
}
